﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PPIDesktopClient.Entities;
using PPIDesktopClient.Entities.Kml;
using PPIDesktopClient.General;
using System.Xml;

namespace PPIDesktopClient.Logic
{
    public class LDistancePositions
    {
        public static EKmlFile toKml(EDistancePositions positions)
        {
            EKmlFile _KmlFile = new EKmlFile();
            string _kmlData ="";
            string _WebAddress = Constants.K_GoogleMapDirectionsKML;
            _WebAddress = _WebAddress.Replace("KML_SOURCE_ADDRESS", positions.PositionOne.GetAddress().Replace(" ", "+"));
            _WebAddress = _WebAddress.Replace("KML_DEST_ADDRESS", positions.PositionTwo.GetAddress().Replace(" ", "+"));
            string _kml = HTMLHelper.readHtmlPage(_WebAddress);
            if (string.IsNullOrEmpty(_kml))
            {
                _kmlData = ""; 
            }
            else
            {
                int _StartingIndex = _kml.IndexOf("<Document>") + "<Document>".Length;
                _kmlData = _kml.Substring(_StartingIndex, _kml.Length - _StartingIndex);
                _StartingIndex = _kmlData.IndexOf("</Document>");
                _kmlData = _kmlData.Substring(0, _StartingIndex);
            }
            _KmlFile.KMLCode = _kmlData;
            return _KmlFile;



        }




    }
}
